﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;

namespace WidgetLibrary
{
  
    class Configuration : INotifyPropertyChanged
    {
        #region Properties

        //Save Widget configuration.
        private string _ConfigurationItem = "";
        private string ConfigurationItem
        {
            get
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("ConfigurationItem"))
                {
                    if (IsolatedStorageSettings.ApplicationSettings["ConfigurationItem"] != null)
                    {
                        _ConfigurationItem = IsolatedStorageSettings.ApplicationSettings["ConfigurationItem"] as string;
                        if (_ConfigurationItem != null) return _ConfigurationItem;
                    }
                }
                return "";
            }
            set
            {
                IsolatedStorageSettings.ApplicationSettings["ConfigurationItem"] = value;
                NotifyPropertyChanged("ConfigurationItem");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        public Configuration() { }

        /// <summary> Save Widget's Configuration in Isolated Storage </summary>
        /// <param name="config"> The Widget deserialized string with config.</param>
        public void SaveConfiguration(string config)
        {
            this.ConfigurationItem = config;
        }        
       

        /// <summary> Load the deserialized string with config stored in Isolated Storage and pass it to Widget</summary>
        /// <param name="widget"> The Widget wich needs the Configuration</param>
        public void LoadConfiguration(WebBrowser widget)
        {
            try
            {
                string data = this.ConfigurationItem;
                //string param = "widget.setdata("+data+")";//(" + data + ")"; 
                widget.InvokeScript("setdata",  new string[] { data } );
            }
            catch (Exception e)
            {
                string message = e.Message;
            }
        }

    }
}
